package com.jetpackframework.webview.commands;

import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import com.gwm.annotation.router.AutoService;
import com.jetpackframework.AppHandler;
import com.jetpackframework.ContextUtil;
import com.jetpackframework.IMainProcessToWebViewInsterface;
import com.jetpackframework.webview.IWebViewCommand;

import org.json.JSONException;
import org.json.JSONObject;

@AutoService(IWebViewCommand.class)
public class ShowToast implements IWebViewCommand {
    @Override
    public String commandName() {
        return "showToast";
    }

    @Override
    public void execCommand(String params, String callbackName, IMainProcessToWebViewInsterface mpwv) {
        try {
            Log.e("TAG","handleWebViewCommand params="+params+",callName="+callbackName);
            JSONObject jsonObject = new JSONObject(params);
            final String message = jsonObject.getString("message");
            mpwv.onResult(callbackName,message);
            AppHandler.getAppHandler().post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(ContextUtil.get(),message,Toast.LENGTH_LONG).show();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
